package net.orange.framework.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

import net.orange.architecture.process.ProcessFactory;
import net.orange.architecture.web.BaseController;
import net.orange.architecture.web.SelectItemComparator;
import net.orange.framework.logic.IDepartmentProcess;
import net.orange.framework.model.Department;

/**
 * The department managed bean using in JSF MVC modal. it is include the web
 * page content and control the action and page forward direction in the web
 * site.
 */
@ManagedBean(name = "department")
@SessionScoped
public class DepartmentController extends BaseController {

	/**
	 * The department code want to search.
	 */
	private String sr_dept_code;

	/**
	 * The department name want to search.
	 */
	private String sr_dept_name;

	/**
	 * The select items of the departments
	 */
	private List<SelectItem> departmentList;

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.web.BaseController#getClazz()
	 */
	protected Class getClazz() {
		return Department.class;
	}

	/**
	 * @return Returns the sr_dept_code.
	 */
	public String getSr_dept_code() {
		return sr_dept_code;
	}

	/**
	 * @return Returns the sr_dept_name.
	 */
	public String getSr_dept_name() {
		return sr_dept_name;
	}

	/**
	 * @param sr_dept_code
	 *            The sr_dept_code to set.
	 */
	public void setSr_dept_code(String sr_dept_code) {
		this.sr_dept_code = sr_dept_code;
	}

	/**
	 * @param sr_dept_name
	 *            The sr_dept_name to set.
	 */
	public void setSr_dept_name(String sr_dept_name) {
		this.sr_dept_name = sr_dept_name;
	}

	/**
	 * Return the select item (department) for selection component.
	 * 
	 * @return The select item (department) for selection component.
	 */
	public List<SelectItem> getDepartments() throws Exception {
		if (departmentList != null)
			return departmentList;
		
		departmentList = new ArrayList<SelectItem>();
		IDepartmentProcess process = (IDepartmentProcess) ProcessFactory
				.get(Department.class);

		Collection departments = (Collection) process.listAll();
		departmentList.add(new SelectItem("", ""));

		for (Object department : departments.toArray()) {
			String code = ((Department) department).getDept_code();
			String name = ((Department) department).getDept_name();

			name = repeat("-", code.length() - 1) + name + " (" + code + ")";
			departmentList.add(new SelectItem(code, name));
		}

		Collections.sort(departmentList, new SelectItemComparator());
		return departmentList;
	}

	/**
	 * @return the parent_dept_code
	 */
	public String getParent_dept_code() {
		Department department = (Department) content;
		return (department != null && department.getParent() != null) ? department
				.getParent().getDept_code() : null;
	}

	/**
	 * @param parent_dept_code
	 *            the parent_dept_code to set
	 */
	public void setParent_dept_code(String parent_dept_code) throws Exception {
		Department department = (Department) content;
		IDepartmentProcess process = (IDepartmentProcess) ProcessFactory
				.get(Department.class);
		if (department != null) {
			Department d =(Department) process.find("dept_code",
					parent_dept_code);
			department.setParent(d);
		}
	}

	/**
	 * Repeat the string.
	 * 
	 * @param s
	 *            The original string.
	 * @param t
	 *            The repeat times.
	 * @return the string that repeat t times.
	 * @throws Exception
	 */
	private String repeat(String s, int t) throws Exception {
		String r = "";
		if (s != null)
			for (int i = 0; i < t; ++i)
				r += s;
		return r;
	}
}
